<?php

class Shu extends Controller {

	function Shu()
	{
		parent::Controller();	
		$this->load->library('auth');
		$this->auth->check_user_authentification();
		$this->load->model('shu_model');		
	}
	
	function index()
	{
		$data['title'] = "Master Perhitungan SHU";
		$data['main_content'] = 'shu/display';
		$data['shu_data'] = $this->shu_model->get_all_data();
		$data['jum_shu'] = $this->shu_model->get_jum_data();
		$this->load->view('layout/template', $data);
	}

	function add()
	{
		$data['title'] = "Tambah Master SHU";
		$data['main_content'] = 'shu/form';
		$data['act'] = 'add';
		$data['form_act'] = 'insert';
		$data['shu_data'] = FALSE;
		$data['new_id'] = $this->shu_model->get_last_id();
		//echo'data : ' . $data['new_id'];
		
		$this->load->view('layout/template', $data);
	}

	function view()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Lihat Master SHU";
		$data['main_content'] = 'shu/form';
		$data['act'] = 'view';
		$data['form_act'] = '';
		$data['shu_data'] = $this->shu_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}	

	function edit()
	{
		$id = $this->uri->segment(3);
		$data['title'] = "Edit Master SHU";
		$data['main_content'] = 'shu/form';
		$data['act'] = 'edit';
		$data['form_act'] = 'update/'.$id;
		$data['shu_data'] = $this->shu_model->get_data_by_id($id);
		$this->load->view('layout/template', $data);
	}
	
	function popup()
	{
		$data['shu_data'] = $this->shu_model->get_all_data();
		$this->load->view('shu/popup', $data);
	}

	function insert()
	{
		if (!$this->_shu_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->add();
		}
		else
		{
			$this->shu_model->fill_data();
			//Cek Akun
			if(!$this->shu_model->check_name())
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Ket SHU telah digunakan');
				$this->add();
			}
			//Insert Data
			elseif($this->shu_model->insert_data()) 
			{
				$this->session->set_userdata('SUCCESSMSG', 'SHU baru sukses ;)');
				redirect('shu');
			}			
		}
	}

	function update()
	{
		$id = $this->uri->segment(3);
		if (!$this->_shu_validation())
		{
			$this->session->set_userdata('ERRMSG_ARR', validation_errors());
			$this->edit();
		}
		else
		{
			$this->shu_model->fill_data();
			//Cek Akun
			if(!$this->shu_model->check_name($id))
			{
				$this->session->set_userdata('ERRMSG_ARR', 'Ket SHU telah digunakan');
				$this->edit();
			}
			//Update Data
			elseif($this->shu_model->update_data($id))
			{
				$this->session->set_userdata('SUCCESSMSG', 'Update shu sukses ;)');
				redirect('shu');
			}
		}
	}

	function delete()
	{	
		$id = $this->input->post('id');
		$shu_data = $this->shu_model->get_data_by_id($id);		
		
			if($this->shu_model->delete_data($id))
			{
				$msg = 'S#Shu '.$shu_data['id_shu'].' telah dihapus.';
			}
			else
			{
				$msg .= 'E#Terjadi kesalahan dalam menghapus data shu '.$shu_data['id_shu'].'. Harap coba lagi.';
			}
			echo $msg;
	}

	function _shu_validation()
	{	
		$this->form_validation->set_rules('id_shu', 'ID shu', 'trim|required');
		$this->form_validation->set_rules('nama_shu', 'Ket SHU', 'trim|required');
		$this->form_validation->set_rules('persentase', 'Persentase', 'trim|required');
		//$this->form_validation->set_rules('nilai_buku', 'Nilai Buku', 'trim|required');
			
		return $this->form_validation->run();
	}

}
/* End of file shu.php */
/* Location: ./application/controllers/shu.php */
